<?php
declare(strict_types=1);

namespace Service\UserService;
use Service\BaseService;
use App\Model\BbcModel\OpenId;
use Service\MemberService\VipService;
use Service\Code\Normal;
use Service\VenderClient\Hashids\Hashids;

/**
 * 用户授权服务中心
 * @author chenshubo
 */
class UserAuthLoginService extends BaseService
{
    /**
     * user login encrypt iv
     * @access private
     * @var    string
     */
    const USER_LOGIN_ENCRYPT_IV = 'usersAutoLoginIv';
    /**
     * user login token salt
     * @access private
     * @var    string
     */
    const USER_TOKEN_SALT = 'userTokenSalt';
    
    //加密方法
    const OPENSSL_METHOD = 'AES-256-CBC';
    
    //OPENID 缓存对象
    const CACHE_USER_OPEN_ID_KEY = 'USER_OPENID_KEY';
    const CACHE_USER_OPEN_ID_EXPIRE_TIME = 3600 * 24 * 365;
    
    //绑定天数
    const USER_BIND_DAYS = 365;
    
    
    /**
     * 绑定自动登录
     * @param string $client
     * @param int $cid
     * @param int $mid
     * @param int $mnid
     * @param int $mn_sid
     * @param array $param
     * @return mixed
     */
    public function bindAutoLogin(string $client,int $cid,int $mid,int $mnid,int $mn_sid,array $param)
    {
        $token_info   = [
            'mid'       => $mid,
            'client'    => $client,
            'mnid'      => $mnid,
            'mn_sid'    => $mn_sid,
            'cid'       => $cid,
            'timestamp' => time(),
            'randstr'   => getRandChar('64')
        ];
        $token_str              = json_encode($token_info,JSON_UNESCAPED_UNICODE);
        $token                  = strtoupper(sha1($token_str.self::CACHE_USER_OPEN_ID_KEY));
        $param['token']         = $token;
        $param['expir_time']    = $token_info['timestamp'] + self::CACHE_USER_OPEN_ID_EXPIRE_TIME;
        
        //绑定自动登录
        $result_bind = WxBindLoginService::instance()->bind($client, $cid, $mnid, $mn_sid, $mid, $param);
        if ( $result_bind['code'] != Normal::SUC) {
            return $result_bind;
        }
        
        $cache_key = $this->getTokenKey($token);
        redis()->set($cache_key,$token_str,self::CACHE_USER_OPEN_ID_EXPIRE_TIME);
        
        return Normal::returnCode(Normal::SUC,"登录成功",[
            'token'     => $token,
            'is_vip'    => 0,
        ]);
    }
    
    /**
     * 获取tokenKey
     * @param string $token
     */
    public function getTokenKey(string $token)
    {
        return self::CACHE_USER_OPEN_ID_KEY.":{$token}";
    }
    
    /**
     * 获取登录信息
     * @param string $token
     */
    public function getLoginByToken(string $token)
    {
        $value = redis()->get($this->getTokenKey($token));
        $cache = $value ? @json_decode($value,true) : [];
        return $cache;
    }
    
    /**
     * 清除登录状态
     * @param string $token
     */
    public function removeUserLogin(string $token)
    {
        return redis()->del($this->getTokenKey($token));
    }
    
    /**
     * 加密key
     * @param  string $encrypt_str
     * @access private
     * @return string
     */
    private function _keyEncrypt(string $encrypt_str)
    {
        return openssl_encrypt($encrypt_str,self::OPENSSL_METHOD, self::USER_TOKEN_SALT, 0, self::USER_LOGIN_ENCRYPT_IV);
    }
    
    /**
     * 解密key
     * @param  string $decrypt_str
     * @access private
     * @return string
     */
    private function _keyDecrypt(string $decrypt_str)
    {
        return openssl_decrypt($decrypt_str,self::OPENSSL_METHOD, self::USER_TOKEN_SALT, 0, self::USER_LOGIN_ENCRYPT_IV);
    }
    
    /**
     * 生成openid
     * @param  string $str
     * @access private
     * @return string
     */
    private function _generateOpenId(string $str)
    {
        return md5(self::USER_TOKEN_SALT . md5($str) . self::USER_LOGIN_ENCRYPT_IV);
    }
}